package Client.GUI;

//import javafx.scene.text.Font;
import javax.swing.*;

import java.net.*;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.StyledEditorKit.BoldAction;
import javax.swing.text.StyledEditorKit.ItalicAction;

import Client.VerbindungsAufbauClient;
import Server.HaggisServer;
import Server.VerbindungsAufbauServer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.font.*;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 
 * @author Alex
 *
 */

public class SpielmodusGUI extends JFrame {
	

	
	// JPanel f�r den Hauptbereich des JFrames
	private JPanel mainpanel = new JPanel();
	
	//3 Buttons
	public static JRadioButton zweispieler = new JRadioButton ("2 Spielermodus");
	private JRadioButton dreispieler = new JRadioButton ("3 Spielermodus");
	public static JButton bestaetigen = new JButton ("OK");
	
	//Menubar und Menuitems
	private JMenuBar menuBar = new JMenuBar();
	private JMenu Helpmenu = new JMenu("Help");
	private JMenuItem Instructionsmenu = new JMenuItem("Anleitung");
	private JMenuItem Exit = new JMenuItem("Exit");
	
	//Slider
	private static JSlider punktzahlslider = new JSlider();
	
	//6 Labels
	private JLabel spielmodus = new JLabel ("Spielmodus");
	private JLabel IPadresse = new JLabel ("IP Adresse");
	private JLabel ShowIP = new JLabel ("");
	
	private JLabel punktzahllabel = new JLabel ("Zu erreichende Punktzahl");
	
	private JLabel Spacer = new JLabel ("");
	private JLabel PortLabel = new JLabel("      Port eingeben ");
	private JLabel NameLabel = new JLabel(" Username eingeben "); 
	
	
	//Textfield
	private static JTextField Porttextfield = new JTextField("4444", 16);
	public static JTextField Usernametextfield = new JTextField("Spieler 1", 16);
	
	// Panel 1: Fuer die beiden Labels
	private JPanel panel1 = new JPanel();
	
	// sub panels von panel 1
	private JPanel panel11 = new JPanel();
	private JPanel panel12 = new JPanel();
	private JPanel panel13 = new JPanel();
	
	
	// Panel 2: Fuer die beiden Buttons
	private JPanel panel2 = new JPanel();
	//subpanel von panel 2
	private JPanel panel21 = new JPanel();
	private JPanel panel22 = new JPanel();
	private JPanel panel23 = new JPanel();

	
	// Panel 3: Fuer Label, SLider und Button
	private JPanel panel3 = new JPanel();
	
	//subpanels von panel 3
	private JPanel panel31 = new JPanel();
	private JPanel panel32 = new JPanel();
	private JPanel panel33 = new JPanel();
	
	
	
	//konstruktor
	public SpielmodusGUI() throws UnknownHostException {
		this.setTitle("Haggis");
//		setPreferredSize(new Dimension(MainClass.getWidth(),MainClass.getHeight()));
		setPreferredSize(new Dimension(850, 450));
		setBounds(BildschirmGroesse.getWidth()/2-425, BildschirmGroesse.getHeight()/2-225, 550, 200); // Platzierung und groesse des Main Framesetzen
		setResizable(true);
		setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
		//Slider 
		punktzahlslider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				int punktzahl = punktzahlslider.getValue();
				
			}
		});
		punktzahlslider.setMinorTickSpacing(5);
		punktzahlslider.setMajorTickSpacing(10);
		punktzahlslider.setMinimum(10);
		punktzahlslider.setMaximum(350);
		punktzahlslider.setPreferredSize(new Dimension (780, 60));
		punktzahlslider.setSnapToTicks(true);
		punktzahlslider.setPaintTicks(true);
		punktzahlslider.setPaintLabels(true);
		
		
		//Layout manager
		mainpanel.setLayout(new BorderLayout());
		getContentPane().add(mainpanel);
		
		panel1.setLayout(new BorderLayout());
		panel3.setLayout(new BorderLayout());
		panel2.setLayout(new BorderLayout());
		//panel2.setLayout(new GridBagLayout());

		
		//fill panel1 with the subpanels in north and south
		panel1.add(panel12, BorderLayout.NORTH);
		panel1.add(panel11, BorderLayout.CENTER);
		panel1.add(panel13, BorderLayout.SOUTH);
		
		
		//set panel2 in the middle of the screen with gridlayout	
//		panel2.setLayout(new GridBagLayout());
//        panel2.setBorder(BorderFactory.createEmptyBorder(5, 10, 10, 5));
		
		panel2.add(panel23, BorderLayout.NORTH);
		panel2.add(panel21, BorderLayout.CENTER);
		panel2.add(panel22, BorderLayout.SOUTH);
     
        
		
		//fill panel3 with the subpanels in north and south and center
		panel3.add(panel31, BorderLayout.NORTH);
		panel3.add(panel32, BorderLayout.CENTER);
		panel3.add(panel33, BorderLayout.SOUTH);
		
		//fill the mainpanel with 3 panels-- panel1, panel2 and panel3
		mainpanel.add(panel1, BorderLayout.NORTH);
		mainpanel.add(panel2, BorderLayout.CENTER);
		
		mainpanel.add(panel3, BorderLayout.SOUTH);
		
		
		//fill the subpanels with the components
		
		panel12.add(spielmodus);
		panel11.add(IPadresse);
		panel11.add(ShowIP);
		panel13.add(PortLabel);
		panel13.add(Porttextfield);
		
		
		//Schriftart anpassen
		
		NameLabel.setFont(NameLabel.getFont().deriveFont(14f));
		PortLabel.setFont(IPadresse.getFont().deriveFont(18f));
		PortLabel.setForeground(Color.RED);
		spielmodus.setFont(spielmodus.getFont().deriveFont(24f));
		IPadresse.setFont(IPadresse.getFont().deriveFont(18f));
		ShowIP.setFont(ShowIP.getFont().deriveFont(18f));
		punktzahllabel.setFont(punktzahllabel.getFont().deriveFont(14f));
		zweispieler.setFont(zweispieler.getFont().deriveFont(18f));
		dreispieler.setFont(dreispieler.getFont().deriveFont(18f));
		IPadresse.setForeground(Color.RED);
		ShowIP.setForeground(Color.RED);
		
		//hintergrund gruen
//		panel11.setBackground(new Color(0, 100, 0));
//		panel12.setBackground(new Color(0, 100, 0));
//		panel2.setBackground(new Color(0, 100, 0));
//		panel21.setBackground(new Color(0, 100, 0));
//		
//		panel31.setBackground(new Color(0, 100, 0));
//		panel32.setBackground(new Color(0, 100, 0));
//		panel33.setBackground(new Color(0, 100, 0));
		
		
		//fill the subpanel with components and panel2 with subpanel
		panel21.add(zweispieler);
		panel21.add(dreispieler);
		panel22.add(NameLabel);
		panel22.add(Usernametextfield);
		panel23.add(Spacer);
		//panel2.add(panel21);
		
		
		
		

		
		//fill panel3 with the components
		panel31.add(punktzahllabel);
		panel32.add(punktzahlslider);
		panel33.add(bestaetigen);
		
		//Radiobutton groupen
		ButtonGroup group = new ButtonGroup();
		group.add(zweispieler);
		group.add(dreispieler);
		zweispieler.setSelected(true);
		
		
	
		pack();
		setVisible(true);
		
		
		//IPAdresse holen
		InetAddress testing = InetAddress.getLocalHost();
		String ipadresse = testing.getHostAddress();
		ShowIP.setText(ipadresse);
		
		//MenuBar setzen und Items einf�gen
		setJMenuBar(menuBar);
		menuBar.add(Helpmenu);
		Helpmenu.add(Instructionsmenu);
		Helpmenu.add(Exit);
		Exit.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
			
			
		});
		//as soon as the button "OK" is clicked the server and a client will be started
		SpielmodusGUI.bestaetigen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					
					//starting the server with a Haggisserver Thread
					new HaggisServer().start();
					
					//Client is started
					InetAddress testing = InetAddress.getLocalHost();
					String ipadresse = testing.getHostAddress();
					new VerbindungsAufbauClient().connect(getPorttextfield(), getUsernametextfield());
					
					/**
					 * line 263 has to be activated if you want to play on 2 different notebooks and line 257 must be deactivated
					 */
					//new VerbindungsAufbauClient().connect(getPorttextfield(), getUsernametextfield(), ipadresse);
					
					bestaetigen.setEnabled(false);
					if (bestaetigen.isEnabled() == false) {
						SpielmodusGUI.this.dispose();
						
						
					}
	
				} catch (Exception ex) { // inkorrekte Eingabe
					JOptionPane.showMessageDialog(null,
							"Kann nicht gestartet werden", "Fehler",
							JOptionPane.INFORMATION_MESSAGE); // Dialogbox
				}
			}

//			private void dispose() {
//				// TODO Auto-generated method stub
//				
//			}
		});}

//test
	
	

		
	
	public static int getPorttextfield() {
		String s = Porttextfield.getText();
		int portnumber = (Integer.parseInt(s));
		return portnumber;
	}



	public static String getUsernametextfield() {
		return Usernametextfield.getText();
	}
	
	

//	public static void main(String[] args) throws UnknownHostException {
//		SpielmodusGUI fenster = new SpielmodusGUI();
//		}
//	
	
	

	public static int getPunktzahlslider() {
		int punkte = punktzahlslider.getValue();
		return punkte;
	}

	public void actionPerformed(ActionEvent arg0) {
		
		

	}
	
	

}
